<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索框 -->
          <div style="margin-top: 6px">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              @keyup.enter.native="getGoodsList"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getGoodsList"
              ></el-button>
            </el-input></div
        ></el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <el-table :data="goodsList" :row-class-name="tableRowClassName" border>
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" >
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" >
        </el-table-column>
        <el-table-column label="创建时间" width="150">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import '@/filter/index.js'
export default {
  data() {
    return {
      // 获取数据参数列表对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      goodsList: [],
      total: 0,
      now: '',
    }
  },
  created() {
    this.getGoodsList()
    this.now = +new Date()
  },
  methods: {
    // 获得商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 删除商品
    async delGoodList(id) {
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return
      }
      this.getGoodsList()
    },
    // 表格隔行变色
    tableRowClassName({ rowIndex }) {
      return rowIndex % 2 == 0 ? 'warning-row' : 'success-row'
    },
    // 刪除
    del(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
          this.delGoodList(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 监听pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
  },
}
</script>

<style lang="less" scoped>
::v-deep .el-table .warning-row {
  background: rgb(255, 255, 255);
}

::v-deep .el-table .success-row {
  background: rgb(250, 250, 250);
}
</style>
